from flask import Blueprint, jsonify
from app.extensions import cors, csrf
from .resources.articles import ArticleAPI, ArticlesAPI
from .resources.category import CategoryAPI
from .resources.token import AuthTokenAPI
from .resources.users import UserAPI
from .errors import register_errors

bp = Blueprint('api_v1', __name__)
# 跨域处理
cors.init_app(bp)
# csrf把API的路由都不使用
csrf.exempt(bp)

# 分类API
category_view = CategoryAPI.as_view('categorys')
bp.add_url_rule('/categorys', defaults={'id': None}, view_func=category_view, methods=['GET'])
bp.add_url_rule('/categorys/<int:id>', view_func=category_view, methods=['GET']) 
# 文章API
bp.add_url_rule('/article/<int:id>', view_func=ArticleAPI.as_view('article'), methods=['GET', 'POST', 'DELETE'])
bp.add_url_rule('/articles', view_func=ArticlesAPI.as_view('articles'), methods=['GET', 'POST'])

# 用户API
bp.add_url_rule('/user', view_func=UserAPI.as_view('user'), methods=['GET', 'POST'])
bp.add_url_rule('/oauth/token', view_func=AuthTokenAPI.as_view('token'), methods=['POST'])


# 注册错误信息返回
register_errors(bp)